package com.kkd.kkdapp.adapter;

import java.util.List;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.http.entity.Privilege;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class PrivilegeGridViewAdapter extends
		RecyclerView.Adapter<PrivilegeGridViewAdapter.MyViewHolder> {

	private Context context;
	private List<Privilege> arr;
	private LayoutInflater mInflater;
	private OnItemclick callBack;

	public PrivilegeGridViewAdapter(Context context, List<Privilege> arr) {
		this.context = context;
		this.arr = arr;
		mInflater = LayoutInflater.from(context);
	}

	@Override
	public int getItemCount() {
		return arr.size();
	}

	public class MyViewHolder extends ViewHolder {

		ImageView privilege_img;
		TextView privilege_tv;
		RelativeLayout item_view;

		public MyViewHolder(View view) {
			super(view);
			privilege_img = (ImageView) view.findViewById(R.id.privilege_img);
			privilege_tv = (TextView) view.findViewById(R.id.privilege_tv);
			item_view = (RelativeLayout) view.findViewById(R.id.item_view);
		}
	}

	@Override
	public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
		MyViewHolder holder = new MyViewHolder(mInflater.inflate(
				R.layout.privilegegridview_item, parent, false));
		return holder;
	}

	@Override
	public void onBindViewHolder(MyViewHolder nViewHolder, final int position) {
		if (nViewHolder.privilege_img.getTag() == null
				|| !nViewHolder.privilege_img.getTag().toString()
						.equals(arr.get(position).getImage_small())) {
			ImageLoader.getInstance().displayImage(
					arr.get(position).getImage_small(),
					nViewHolder.privilege_img);
			nViewHolder.privilege_img
					.setTag(arr.get(position).getImage_small());
		}
		// nViewHolder.privilege_img.setImageResource(arr.get(position).getImgUrl());
		if (arr.size() - 1 == position) {
			nViewHolder.privilege_tv.setTextColor(Color.parseColor("#b8b8b8"));
		} else {
			nViewHolder.privilege_tv.setTextColor(Color.parseColor("#333333"));
		}
		nViewHolder.privilege_tv.setText(arr.get(position).getTitle());
		nViewHolder.item_view.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (callBack != null) {
					callBack.getPosition(position);
				}
			}
		});
	}

	public void setOnItemclick(OnItemclick callBack) {
		this.callBack = callBack;
	}

	public interface OnItemclick {
		public void getPosition(int position);
	}
}
